package erp_services

import (
	"errors"
	"xiehui-service/app/models/erp_models"
	"xiehui-service/global"
)

type orderDispatchQueueService struct {
}

var OrderDispatchQueueService = new(orderDispatchQueueService)

// 查询队列
func (orderDispatchQueueService *orderDispatchQueueService) GetOrderDispatchQueue(queue_ids []string, vehicle_ids []string) (orderDispatchQueue []erp_models.OrderDispatchQueue, err error) {
	err = global.App.DB.Where("mixer_no = '' AND queue_id IN ? AND vehicle_id IN ?", queue_ids, vehicle_ids).Find(&orderDispatchQueue).Error
	if err != nil {
		err = errors.New("数据不存在")
	}
	return
}

// 查询拌台排车数量
func (orderDispatchQueueService *orderDispatchQueueService) GetMixtableCars(mixer_no uint) (carCnt int, err error) {
	result := global.App.DB.Where("mixer_no = ?", mixer_no).Find(&[]erp_models.OrderDispatchQueue{})
	if result.Error != nil {
		err = errors.New("数据不存在")
	}
	carCnt = int(result.RowsAffected)
	return
}

// 查询队列第一辆车信息
func (orderDispatchQueueService *orderDispatchQueueService) GetQueueCar(queue_id uint) (orderDispatchQueue erp_models.OrderDispatchQueue, err error) {
	err = global.App.DB.Where("mixer_no = '' AND queue_id = ?", queue_id).First(&orderDispatchQueue).Error
	if err != nil {
		if err.Error() == "record not found" {
			err = errors.New("数据不存在")
		}
	}
	return
}
